package com.meyoung.day2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class WaitTest {
    WebDriver driver;
    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\cll520\\IdeaProjects\\selenium11\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
        //全局等待
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    /*
    打开我们的测试页面
    点击wait按钮
    获取文本，并判断是否“wait for display”
     */
    @Test
    //全局等待
    public void waitTest() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        driver.findElement(By.xpath("//*[@id=\"wait\"]/input")).click();
        //定位元素，并得到文本
        String text = driver.findElement(By.xpath("//*[@id=\"display\"]/div")).getText();
        //判断文本是否正确
        Assert.assertEquals(text,"wait for display");
        Thread.sleep(2000);

    }
    @Test
    //显示等待
    public void waitTest1() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        driver.findElement(By.xpath("//*[@id=\"wait\"]/input")).click();
        //引入新类
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"display\"]/div")));
        //定位元素，并得到文本
        String text = driver.findElement(By.xpath("//*[@id=\"display\"]/div")).getText();
        //判断文本是否正确
        Assert.assertEquals(text,"wait for display");
        Thread.sleep(2000);

    }
    @AfterMethod
    public void closedBrowser(){
        driver.quit();
    }
}
